package com.haikesi.api.menuauthority.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.menuauthority.domain.vo.*;
import com.haikesi.api.menuauthority.service.IMenuAuthorityService;
import com.haikesi.api.organization.domain.vo.OrganizationTreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MenuAuthorityController
 * @Description: 资源管理增删改成 资源配置的查询和添加
 * @author: dx
 * @date: 2019年4月30日 下午12:32:25
 * @version V1.0
 * @Copyright: 2019 重庆海克斯科技有限公司. 注意：本内容仅限于重庆海克斯科技有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
@Api(description = "菜单相关接口", tags = ApiIndex.MenuAuthorityController)
@RequestMapping(value = "/api/menu")
@RestController
public class MenuAuthorityController {
	@Autowired
	private IMenuAuthorityService menuAuthorityService;

	@ApiOperation(value = "菜单列表")
	@GetMapping(value = "/list")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
	public R<PageInfo<List<MenuAuthorityVO>>> list(
	        @ApiParam(value = "查询参数") @ModelAttribute MenuAuthoritySearchVO searchVO) {
        List<MenuAuthorityVO> list = menuAuthorityService.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
		return new R(pageInfo);
	}


	@ApiOperation(value = "查询属性列表")
	@GetMapping(value = "/treeList")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
	public R<List<OrganizationTreeVO>> getTreeList(
			@ApiParam(value = "查询参数") @ModelAttribute MenuAuthorityTreeSearchVO searchVO) {
		List<MenuAuthorityTreeVO> list = menuAuthorityService.getTreeList(searchVO);
		return new R(list);
	}


	@ApiOperation(value = "创建菜单")
	@PostMapping(value = "")
	public R create(@ApiParam(value = "添加参数") @Valid @RequestBody MenuAuthoritySaveVO vo, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			List<Error> messages=new ArrayList<>();
			for(int i=0;i<bindingResult.getFieldErrors().size();i++){
				messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
			}
			return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
		}
		return new R().setData(menuAuthorityService.create(vo));
	}

	@ApiOperation(value = "编辑菜单")
	@PutMapping(value = "")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
	public R update(@ApiParam(value = "修改参数") @Valid @RequestBody MenuAuthorityUpdateVO updateVO, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			List<Error> messages=new ArrayList<>();
			for(int i=0;i<bindingResult.getFieldErrors().size();i++){
				messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
			}
			return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
		}
		menuAuthorityService.update(updateVO);
		return new R();
	}

//	@ApiOperation(value = "删除菜单")
//	@DeleteMapping(value = "")
////    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
////    @Token
//	public R delete(@ApiParam(value = "菜单对应id") @RequestParam String id) {
//
//		int nextMenuCount = menuAuthorityService.getNextMenuCount(id);
//		if (nextMenuCount != 0) {
//			return new R(0, "请先删除下级菜单");
//		} else {
//			menuAuthorityService.deleteMenu(id);
//			return new R();
//		}
//
//	}

	@ApiOperation(value = "删除")
	@DeleteMapping(value = "")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
	public R delete(
			@ApiParam(value = "修改参数") @RequestParam String id) {
		menuAuthorityService.deleteMenu(id);
		return new R();
	}




//	@ApiOperation(value = "获取配置资源列表")
//	@GetMapping(value = "/getResource")
////    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
////    @Token
//	public R<PageInfo<List<UserVO>>> getResource(
//			@ApiParam(value = "权限id") @ModelAttribute GetResouresSearchVO searchVO) {
//		List<GetResourcesPO> resource = RS.getResource(searchVO);
//		PageInfo pageInfo = new PageInfo(resource);
//		return new R(pageInfo);
//	}
//
	@ApiOperation(value = "菜单配置资源")
	@PostMapping(value = "/addResource")
//    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
//    @Token
	public R addResource(@ApiParam(value = "参数") @Valid @RequestBody MenuAuthorityResourceVO menuAuthorityResourceVO, BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			List<Error> messages=new ArrayList<>();
			for(int i=0;i<bindingResult.getFieldErrors().size();i++){
				messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(),bindingResult.getFieldErrors().get(i).getDefaultMessage()));
			}
			return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
		}
		menuAuthorityService.addResource(menuAuthorityResourceVO);
		return new R();
	}

}
